<?php

namespace Addons\OaSystem\Controller;
use Think\ManageBaseController;

class OaReimbursementStandardController extends OaBaseController
{
    var $model;

    public function import(){

        if (IS_POST) {
            $data['file'] = I('post.reimbursementStandard');
            $data['valid_date'] = date('y-m-d h:i:s', time());

            if (!intval($data['file'])) $this->error("数据文件未上传！");


            if ($this->import_standard_data_from_excel($data['file'], $data['valid_date'], $data['comment'])) //import course data from uploaded Excel file.
                $this->success('保存成功！', U ( 'lists'/*'import?model=' . $this->model ['name'], $this->get_param */), 600);
            else
                $this->error('请检查文件格式');
        }
        else{
            $res['title'] = '报销标准表';
            $res['is_must'] = true;
            $res['is_show'] = 1;
            $res['name'] = 'reimbursementStandard';
            $res['type'] = 'file';
            $fields[] = $res;
            $post_url = addons_url ( 'OaSystem://OaReimbursementStandard/import');
            $this->assign('fields',$fields);
            $this->assign('post_url', $post_url);
            $this->assign('import_template','reimbursementStandard.xlsx');
            $this->display();
            //$this->display('import');
        }

    }

    private function import_standard_data_from_excel($file_id, $date = NULL, $comment = NULL){
        if ($date == NULL) return false;
        $data = array();
        $column = array (
            'A'=>'id',
            'B'=>'province',
            'C'=>'city',
            'D'=>'normal',
            'E'=>'high_time',
            'F'=>'high',
            'G'=>'traffic',
            'H'=>'meals'
        );
        $data = importFormExcel($file_id, $column);

        if ($data['status']){
            $row_line = 1;
            foreach ($data['data'] as $row) {
                $row_line = $row_line +1;
                if ($row_line > 2 ){
                    $row['id'] =  intval($row['id']);
                    $row['province'] = str_replace(' ','',$row['province']);
                    $row['city'] = str_replace(' ','',$row['city']);
                    $row['normal'] = str_replace(' ','',$row['normal']);
                    $row['high_time'] = str_replace(' ','',$row['high_time']);
                    $row['high'] = str_replace(' ','',$row['high']);
                    $row['traffic'] =str_replace(' ','',$row['traffic']);
                    $row['meals'] = str_replace(' ','',$row['meals']);
                    if(empty($row['id'])){
                        return true;
                    }
                    $map['id'] = $row['id'];
                    $old = M('OaReimbursementStandard')->where($map)->find();
                    if(empty($old)){
                        M('OaReimbursementStandard')->add($row);
                    }else{
                        M('OaReimbursementStandard')->save($row);
                    }
                }

            }
            return true;
        }else {
            return false;
        }
    }
}